package com.android.fragmentdemo
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.android.fragmentdemo.adapters.DynamicItem03Adapter
import com.hengfoxwolf.commonbase.base.BaseActivity3
import com.android.fragmentdemo.databeans.DataFactoryIp
import com.android.fragmentdemo.databinding.ActivityScrollingBinding
import com.android.fragmentdemo.defineviews.List01DataItemDecoration
import com.android.fragmentdemo.util.StepLoadCollectionUtil

class ScrollingActivity : BaseActivity3<ActivityScrollingBinding>(ActivityScrollingBinding::inflate) {

    private lateinit var mDyAdapter: DynamicItem03Adapter
    private lateinit var mDataSet: MutableList<String>

    override fun init(savedInstanceState: Bundle?) {
        mDyAdapter = DynamicItem03Adapter()
        val dyLayoutManager = LinearLayoutManager(this@ScrollingActivity)
        StepLoadCollectionUtil.init(DataFactoryIp.createStrDataSet(), 15)
        mDyAdapter.itemDataSet = StepLoadCollectionUtil.getInitDataSet()
        mBinding.rvData01.apply {
            layoutManager = dyLayoutManager
            adapter = mDyAdapter
            addItemDecoration(
                List01DataItemDecoration(
                    this@ScrollingActivity,
                    0F
                ).apply {
                    setDrawLastLine(false)
                })

            addOnScrollListener(object : RecyclerView.OnScrollListener() {
                override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                    super.onScrolled(recyclerView, dx, dy)
                    if (recyclerView.layoutManager is LinearLayoutManager) {
                        val position =
                            ((recyclerView.layoutManager) as? LinearLayoutManager)?.findLastCompletelyVisibleItemPosition() ?: -1
                        if (mDyAdapter.itemCount - 1 == position && !StepLoadCollectionUtil.hadLoadFinished()) {
                            Log.d(TAG, "position:${position}")
                            val mLoadDataSet = StepLoadCollectionUtil.loadItem()
                            mDyAdapter.setItemDataSet(mLoadDataSet)
                        }
                    }

                    if (recyclerView.layoutManager is GridLayoutManager) {
                        val position =
                            ((recyclerView.layoutManager) as? GridLayoutManager)?.findLastCompletelyVisibleItemPosition() ?: -1
                        if (mDataSet.size - 1 == position) {

                        }
                    }

                    if (recyclerView.layoutManager is StaggeredGridLayoutManager) {
                        val positionArray =
                            ((recyclerView.layoutManager) as? StaggeredGridLayoutManager)?.findLastCompletelyVisibleItemPositions(intArrayOf()) ?: intArrayOf()

                    }
                }

                override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                    super.onScrollStateChanged(recyclerView, newState)
                }
            })
        }
    }

    override fun onStop() {
        super.onStop()
        StepLoadCollectionUtil.reset()
    }

    companion object {
        const val TAG = "ScrollingActivity"
    }
}